import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";

/**
 * 分片上传
 * @creationDate 2020/02/19
 */
import * as React from 'react';
import { Modal as CpaasModal, Button } from 'antd';
import intl from 'utils/intl';
import { API_HOST } from 'utils/config';
import { getAccessToken } from 'utils/utils';

var FileChunkUpload = function FileChunkUpload(props) {
  var _React$useState = React.useState(false),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      cpaasModalVisible = _React$useState2[0],
      setCpaasModalVisible = _React$useState2[1];

  var title = props.title,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      style = props.style,
      organizationId = props.organizationId,
      modalProps = props.modalProps,
      prefixPatch = props.prefixPatch;
  var accessToken = getAccessToken();
  var iframeUrl = React.useMemo(function () {
    var configure = {
      href: '',
      search: ''
    };
    configure.href = "".concat(API_HOST).concat(prefixPatch, "/v1/").concat(organizationId, "/upload");
    var searchObject = {
      access_token: accessToken,
      templateCode: props.templateCode
    };
    configure.search = Object.keys(searchObject).filter(function (searchKey) {
      return searchObject[searchKey] !== undefined;
    }).map(function (searchKey) {
      return "".concat(searchKey, "=").concat(searchObject[searchKey]);
    }).join('&');
    return "".concat(configure.href, "?").concat(configure.search);
  }, [organizationId, accessToken, JSON.stringify(props)]);
  var wrapStyle = React.useMemo(function () {
    return _objectSpread({
      height: '100%',
      width: '100%',
      border: 0
    }, style);
  }, [style]);
  var handleFileChunkUploadClick = React.useCallback(function () {
    if (disabled) {
      return;
    }

    setCpaasModalVisible(true);
  }, [iframeUrl, disabled, setCpaasModalVisible, wrapStyle]);
  var closeCpaasModal = React.useCallback(function () {
    setCpaasModalVisible(false);
  }, [setCpaasModalVisible]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
    icon: "upload",
    onClick: handleFileChunkUploadClick
  }, intl.get("cimp.comment.view.button.pageUpload").d('分片上传')), cpaasModalVisible && /*#__PURE__*/React.createElement(CpaasModal, Object.assign({
    visible: true,
    destroyOnClose: true,
    title: title,
    wrapClassName: "ant-modal-sidebar-right",
    transitionName: "move-right"
  }, modalProps, {
    onCancel: closeCpaasModal,
    onOk: closeCpaasModal
  }), /*#__PURE__*/React.createElement("iframe", {
    style: wrapStyle,
    src: iframeUrl,
    title: title
  })));
};

export default FileChunkUpload;